import {  getCates } from '@/api'
export default {
  // 命名空间
  namespace: 'count',
  // 存储状态
  state: {
    num: 0,
    cates: []
  },
  // reducer 修改状态 一定返回新的状态
  reducers: {
    add(state: any, {data}: {type: string, data: number}) {
      state.num += data;
      return {
        ...state
      }
    },
    initCates (state: { cates: any; }, action: { data: any; }) {
      state.cates = action.data;
      return {
        ...state
      }
      
    }
  },
  // 处理异步
  effects: {
    // put 请求成功后 触发 action 到 reducer 修改状态 call 调用异步函数
    *fetchCates(_action: any, { call, put }: any) {      
      const res = yield call(getCates, _action.params);
      if (res.data.code === 0) {
        // 请求成功 dispatch 普通 reducer 修改状态
        yield put({ type: 'initCates',data: res.data.data });
      }
      
    },
  },
};
        